package database

import (
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"notabug.org/ropemann/imabon/src/common"
)

type MongoTestSuite struct {
	suite.Suite
}

func (s *MongoTestSuite) SetupTest() {
	common.LoadConfig("../../.env")
}

func (s *MongoTestSuite) TestMongoDbConn() {
	conn := NewMongoDBConn()
	assert.NotNil(s.T(), conn.Client)
}

func (s *MongoTestSuite) TestMongoDbConnFail() {
	os.Setenv("DB_PORT", "invalid")
	assert.Panics(s.T(), func() { NewMongoDBConn() })
}

func (s *MongoTestSuite) TestConnect() {
	conn := NewMongoDBConn()
	conn.Connect()
	assert.NotNil(s.T(), conn.Ctx)
	assert.NotNil(s.T(), conn.CancelCtx)
}

func (s *MongoTestSuite) TestClose() {
	conn := NewMongoDBConn()
	conn.Connect()
	assert.NotPanics(s.T(), func() { conn.Close() })
}

func TestMongoTestSuite(t *testing.T) {
	suite.Run(t, new(MongoTestSuite))
}
